//金币 看视频赚钱
var gold_video = {
  is_over_today: false,
  go_page_from_home() {
    if (this.is_over_today) {
      console.log("gold_video after_in_task: 今天的gold_video已结束");
      return;
    }
    if (!!utils.exists_text("视频") && !!utils.exists_text("小说")) {
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页任务"])
      );
      console.log("gold_video go_page_from_home: 进入任务");
      sleep(5 * 1000);
      this.after_in_task();
    } else {
      console.log("gold_video go_page_from_home: NOT FOUND");
    }
  },
  after_in_task() {
    if (utils.click_text_starts_with("立即收下")) {
      console.log("gold_video after_in_task: 找到--立即收下--");
      sleep(constant.min_click_after_time * 1000);
      if (this.is_over_today) {
        console.log("gold_video after_in_task: 今天的gold_video已结束");
        return;
      }
      if (utils.click_text_starts_with("金币收益")) {
        console.log("gold_video after_in_task: 找到--金币收益--");
        sleep(constant.min_click_after_time * 1000);
        this.start();
      } else {
        console.log("gold_video after_in_task: 没找到--金币收益--");
      }
      return;
    }
    if (utils.click_text_starts_with("开心收下")) {
      console.log("gold_video after_in_task: 找到--开心收下--");
      sleep(constant.min_click_after_time * 1000);
      if (this.is_over_today) {
        console.log("gold_video after_in_task: 今天的gold_video已结束");
        return;
      }
      if (utils.click_text_starts_with("金币收益")) {
        console.log("gold_video after_in_task: 找到--金币收益--");
        sleep(constant.min_click_after_time * 1000);
        this.start();
      } else {
        console.log("gold_video after_in_task: 没找到--金币收益--");
      }
      return;
    }
    var ad_texts = ["再看一个领取", "观看视频再得", "看广告再"];
    for (var i = 0; i < ad_texts.length; i++) {
      var content = ad_texts[i];
      var ui_text = utils.exists_text_starts_with(content);
      if (ui_text) {
        console.log(
          "gold_video after_in_task: 找到【" + content + "】" + ui_text.text()
        );
        var gold = utils.get_gold(ui_text.text());
        console.log("gold_video after_in_task: " + gold + "金币");
        if (gold >= constant.min_gold) {
          utils.click_text_starts_with(content);
          sleep(constant.min_click_after_time * 1000);
          ad_util.execute();
          this.next(true);
          return;
        } else {
          console.log(
            "gold_video after_in_task: 金币数量 --" + gold + "-- 太少，跳过"
          );
          utils.click_point.apply(
            utils,
            utils.get_size_point(point_map["进入任务之后的关闭"])
          );
          sleep(constant.min_click_after_time * 1000);
          this.next(true);
          return;
        }
      }
    }
    console.log(
      "gold_video after_in_task: 没找到 " +
        ad_texts
          .map(function (i) {
            return "--" + i + "--";
          })
          .join("或者")
    );
    if (this.is_over_today) {
      console.log("gold_video after_in_task: 今天的gold_video已结束");
      return;
    }
    if (utils.click_text_starts_with("金币收益")) {
      console.log("gold_video after_in_task: 找到--金币收益--");
      sleep(constant.min_click_after_time * 1000);
      this.start();
    } else {
      console.log("gold_video after_in_task: 没找到--金币收益--");
    }
  },
  start() {
    console.log("gold_video start");
    if (utils.exists_text_starts_with("看文章赚金币")) {
      this.is_over_today = true;
      this.next();
      return;
    }
    this.init();
  },
  init() {
    var ui_text = utils.exists_text_starts_with("看视频赚");
    if (ui_text) {
      console.log("gold_video init: 找到【看视频赚】" + ui_text.text());
      var gold = utils.get_gold(ui_text.text());
      console.log("gold_video init: " + gold + "金币");
      if (gold > constant.min_gold) {
        utils.click_text_starts_with("看视频赚");
        sleep(constant.min_click_after_time * 1000);
        ad_util.execute(
          () => {
            return utils.exists_text_starts_with("看视频赚");
          },
          (a, b) => {
            utils.click_text_starts_with("看视频赚");
            sleep(constant.min_click_after_time);
            ad_util.execute(a, b);
          }
        );
        this.next();
      } else {
        console.log("gold_video init: 广告金币太少");
        sleep(constant.min_click_after_time * 1000);
        this.next();
      }
    } else {
      console.log("gold_video init: 没找到【看视频赚】");
      this.next();
    }
  },
  next(after_in_task) {
    console.log("gold_video next");
    if (utils.click_text("开心收下")) {
      console.log("gold_video next: 找到【开心收下】");
      sleep(constant.min_click_after_time * 1000);
    }
    if (utils.click_text("立即收下")) {
      console.log("gold_video next: 找到【立即收下】");
      sleep(constant.min_click_after_time * 1000);
    }
    if (after_in_task) {
      this.after_in_task();
    } else {
      back();
      sleep(2 * 1000);
      if (utils.click_text("开心收下")) {
        console.log("gold_video next: 找到【开心收下】");
        sleep(constant.min_click_after_time * 1000);
      }
      if (utils.click_text("立即收下")) {
        console.log("gold_video next: 找到【立即收下】");
        sleep(constant.min_click_after_time * 1000);
      }
      this.after_in_task();
    }
  },
};
